//
//  MusicLibraryViewController.m
//  MusciPlayer
//
//  Created by dlios on 15-5-21.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "MusicLibraryViewController.h"
#import "RankViewController.h"
#import "RecommendViewController.h"
#import "SingerViewController.h"
#import "RadioViewController.h"
#import "MyInfoViewController.h"
#import "SearchViewController.h"
#import "SANetWorkingTask.h"
#import "AppDelegate.h"

@interface MusicLibraryViewController ()<UIScrollViewDelegate>
@property (nonatomic, retain) RecommendViewController *recommend;
@property (nonatomic, retain) RankViewController *rank;
@property (nonatomic, retain) SingerViewController *singer;
@property (nonatomic, retain) RadioViewController *Radio;
@property (nonatomic, retain) UIScrollView *scrollView;
@property (nonatomic, retain) UIImageView * myImageView;
@property (nonatomic, retain) UIView *headerView;
@property (nonatomic, retain) UIView *rollView;
@property (nonatomic, retain) NSMutableArray *buttonArray;
@end

@implementation MusicLibraryViewController
- (void)dealloc
{
  
    [_recommend release];
    [_Radio release];
    [_rank release];
    [_singer release];
    [_scrollView release];
    [_rollView release];
    [_buttonArray release];
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.buttonArray = [NSMutableArray array];
    }
    return self;
}

-(void)viewWillAppear:(BOOL)animated
{
    
    [super viewWillAppear:YES];
    [self.myImageView removeFromSuperview];
    
//    self.navigationItem.title = @"音乐库";
//    self.navigationController.navigationBar.translucent = NO;
    
//    self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:200/255.0f green:200/255.0f blue:184/255.0f alpha:1.0f];
    
    [self.view setBackgroundColor:[UIColor whiteColor]];
//
    self.myImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
//

    NSUserDefaults *aa = [NSUserDefaults standardUserDefaults];


    if ([aa objectForKey:@"pic1"]) {
    
//    if (aa) {
        UIImage *image = [UIImage imageNamed:[aa objectForKey:@"pic1"]];
        
        [_myImageView setImage:image];
    } else {
        [_myImageView setImage:[UIImage imageNamed:@"PIC2.png"]];
        
    }
    
    [self.view addSubview:_myImageView];
    [self.view bringSubviewToFront:self.scrollView];
    [self.view bringSubviewToFront:self.headerView];
//        self.myImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
//        [self.view addSubview:_myImageView];
//    
//        [_myImageView release];
    
    
    
//    // 创建右边的barButtonItem
//    UIBarButtonItem *search = [[UIBarButtonItem alloc] initWithTitle:@"搜索" style:UIBarButtonItemStylePlain target:self action:@selector(searchAction:)];
//    search.tintColor = [UIColor redColor];
//    self.navigationItem.rightBarButtonItem = search;
//    
//    // 创建左边的barButtonItem
//    UIBarButtonItem *myInfo = [[UIBarButtonItem alloc] initWithTitle:@"我的" style:UIBarButtonItemStylePlain target:self action:@selector(myInfoAction:)];
//    myInfo.tintColor = [UIColor redColor];
//    self.navigationItem.leftBarButtonItem = myInfo;
//    [self createView];
//    [self createScrollView];
//    
//    self.rollView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height / 667 * 38, self.view.frame.size.width / 4, self.view.frame.size.height / 667 * 2)];
//    [self.rollView setBackgroundColor:[UIColor redColor]];
//    [self.view addSubview:self.rollView];
//    [self.rollView release];
    
}

- (void)loadView
{
    [super loadView];
//    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, self.view.frame.size.height)];
//    
//    
//    UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];
//    [view1 addGestureRecognizer:pan];
//    [self.view addSubview:view1];
    
    
    
}

#pragma mark - 滑动抽屉
- (void)panAction:(UIPanGestureRecognizer *)pan
{
    AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    
    self.siderViewController = [delegate  siderViewController];
    
    [_siderViewController showLeftViewController:true];
    
}



- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"音乐库";
    self.navigationController.navigationBar.translucent = NO;



    // 创建右边的搜索按钮
    UIButton *search = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width/375.0 * 100,self.view.frame.size.height / 667.0* 20, self.view.frame.size.width / 375 * 30, self.view.frame.size.width / 375 * 49)];
    [search setImage:[UIImage imageNamed:@"iconfont-sousuo.png"] forState:UIControlStateNormal];
    [search addTarget:self action:@selector(searchAction:) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:search];
    
    // 创建左边的我的按钮
    UIButton *my = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width / 375 * 30, self.view.frame.size.width / 375 * 49)];
    [my setImage:[UIImage imageNamed:@"iconfont-wode.png"] forState:UIControlStateNormal];
    [my addTarget:self action:@selector(myInfoAction:) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:my];

    
    [self createScrollView];
    [self createView];
    self.rollView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height / 667 * 38, self.view.frame.size.width / 4, self.view.frame.size.height / 667 * 2)];
    [self.rollView setBackgroundColor:[UIColor whiteColor]];
    [self.headerView addSubview:self.rollView];
    [self.rollView release];

    

    
}

// 搜索按钮的事件
- (void)searchAction:(id)sender
{
    SearchViewController *search = [[SearchViewController alloc] init];
    [self.navigationController pushViewController:search animated:YES];
    
}

// 我的信息按钮的事件
- (void)myInfoAction:(id)sender
{
    MyInfoViewController *myInfo = [[MyInfoViewController alloc] init];
    [self.navigationController pushViewController:myInfo animated:YES];
//    [myInfo release];
}

- (void)createView
{
    self.headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height / 667 * 40)];
    [self.view addSubview:self.headerView];
    
    // 创建数组，存放标题
    NSArray *array = @[@"推荐", @"排行", @"歌手", @"电台"];
    for (int i = 0; i < 4; i++) {
        // 循环创建4个button，分别赋值
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 4 * i, 0, self.view.frame.size.width / 4, self.view.frame.size.height / 667 * 38)];
        [button setTitle:array[i] forState:UIControlStateNormal];
        button.tag = i + 1;
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [_headerView addSubview:button];
        [self.buttonArray addObject:button];
        if (i == 0) {
            [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        }
    }
    
    [_headerView release];

}

#pragma mark - button的点击事件
- (void)buttonAction:(id)sender
{
    // 从上面获取button的tag值，计算scrollView的偏移量
    UIButton *button = (UIButton *)sender;
    [UIView animateWithDuration:0.3 animations:^{
        self.scrollView.contentOffset = CGPointMake(self.view.frame.size.width * (button.tag - 1), 0);
    }];
    [UIView animateWithDuration:0.3 animations:^{
        self.rollView.frame = CGRectMake(self.view.frame.size.width / 4 * (button.tag - 1), self.view.frame.size.height / 667 * 38, self.view.frame.size.width / 4, self.view.frame.size.height / 667 * 2);
    }];
    // 滑动改变按钮颜色
    for (UIButton *button in self.buttonArray) {
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
}

- (void)createScrollView
{
    // 创建4个子视图
    self.recommend = [[RecommendViewController alloc] init];
    self.rank = [[RankViewController alloc] init];
    self.singer = [[SingerViewController alloc] init];
    self.Radio = [[RadioViewController alloc] init];
    [self addChildViewController:self.recommend];
    [self addChildViewController:self.Radio];
    [self addChildViewController:self.rank];
    [self addChildViewController:self.singer];
    
    // 创建scrollView
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height / 667 * 40, self.view.frame.size.width, self.view.frame.size.height - self.view.frame.size.height / 667 * 40)];
    _scrollView.pagingEnabled = YES;
    self.scrollView.delegate = self;
    self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width * 4,0);
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.bounces = NO;
    
    [self.scrollView addSubview:self.recommend.view];
    [self.scrollView addSubview:self.rank.view];
    [self.scrollView addSubview:self.singer.view];
    [self.scrollView addSubview:self.Radio.view];
    
    // 分别设置子视图的size
    self.recommend.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    self.rank.view.frame = CGRectMake(self.view.frame.size.width * 1, 0, self.view.frame.size.width, self.view.frame.size.height);
    self.singer.view.frame = CGRectMake(self.view.frame.size.width * 2, 0, self.view.frame.size.width, self.view.frame.size.height);
      self.Radio.view.frame = CGRectMake(self.view.frame.size.width * 3, 0, self.view.frame.size.width, self.view.frame.size.height);
    
    [self.view addSubview:self.scrollView];
    
    [_recommend release];
    [_rank release];
    [_Radio release];
    [_singer release];
    [self.scrollView release];
}

//- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
//{
//    [UIView animateWithDuration:0.3 animations:^{
//        self.rollView.frame = CGRectMake(self.view.frame.size.width / 4 * (self.num - 1), 38, self.view.frame.size.width / 4, 2);
//    }];
//}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
        [UIView animateWithDuration:0.3 animations:^{
            self.rollView.frame = CGRectMake(self.scrollView.contentOffset.x / 4, self.view.frame.size.height / 667 *38, self.view.frame.size.width / 4, self.view.frame.size.height / 667 * 2);
        }];
}

@end
